package edu.bbu.commonproject.desktop.client.services.thrift.users;

import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;

import edu.bbu.commonproject.desktop.client.exceptions.DataBaseErrorException;
import edu.bbu.commonproject.desktop.client.exceptions.DecryptErrorException;
import edu.bbu.commonproject.desktop.client.exceptions.EmailSendingException;
import edu.bbu.commonproject.desktop.client.exceptions.NoConnectionException;
import edu.bbu.commonproject.desktop.client.exceptions.WrongUserDataException;
import edu.bbu.commonproject.desktop.client.factory.thrift.ThriftCLTFactory;
import edu.bbu.commonproject.desktop.client.model.UserPOJO;
import edu.bbu.commonproject.desktop.client.services.UserCLTServices;
import edu.bbu.commonproject.desktop.client.services.thrift.exceptions.TDatabaseErrorException;
import edu.bbu.commonproject.desktop.client.services.thrift.exceptions.TDecryptErrorException;
import edu.bbu.commonproject.desktop.client.services.thrift.exceptions.TEmailSendingException;
import edu.bbu.commonproject.desktop.client.services.thrift.exceptions.TWrongUserDataException;
import edu.bbu.commonproject.desktop.client.utils.ThriftExecutor;

/**
 * The thrift client, user services implementation.
 */
public class ThriftUserCLTServices implements UserCLTServices
{
	/**
	 * Logging on the user.
	 * @param user the user.
	 * @return true if the logon succesfull, false otherwise.
	 * @throws NoConnectionException if the connaction lost.
	 */
	public void logonUser(UserPOJO user) throws NoConnectionException, WrongUserDataException, DataBaseErrorException
	{
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			ThriftUser thriftUser = new ThriftUser(user.getUserName(), user.getUserPasword());
			
			thriftExecutor.open();
			thriftExecutor.getExecutor().logonUser(thriftUser);
			
		}
		catch (TWrongUserDataException e)
		{
			throw new WrongUserDataException("Wrong username or password.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
		
	}
	
	/**
	 * Registering a new user.
	 * @param user the user.
	 * @return true if the registering succesfull, false otherwise.
	 * @throws NoConnectionException if the connaction lost.
	 */
	public void registerUser(UserPOJO user) throws NoConnectionException, DecryptErrorException, WrongUserDataException, DataBaseErrorException, EmailSendingException
	{
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
			
		try
		{
			ThriftUser thriftUser = new ThriftUser(user.getUserName(), user.getUserPasword());
			
			thriftExecutor.open();
			thriftExecutor.getExecutor().registerUser(thriftUser);
		}
		catch (TDecryptErrorException e)
		{
			throw new DecryptErrorException("Error occured, on password decrypting.");
		}
		catch (TWrongUserDataException e)
		{
			throw new WrongUserDataException("Wrong username or password.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TEmailSendingException e)
		{
			throw new EmailSendingException("Email sending failed.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
		
	}
	
	/**
	 * Changing the user password.
	 * @param user the user.
	 * @param newPassword the nem password.
	 * @return true if the change succesfull, false otherwise.
	 * @throws NoConnectionException if the connaction lost.
	 * @throws WrongUserDataException 
	 */
	public void changePassword(UserPOJO user, String newPassword) throws NoConnectionException, WrongUserDataException, DataBaseErrorException
	{
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
				
		try
		{
			ThriftUser thriftUser = new ThriftUser(user.getUserName(), user.getUserPasword());
			
			thriftExecutor.open();
			thriftExecutor.getExecutor().changePassword(thriftUser, newPassword);
		}
		catch (TWrongUserDataException e)
		{
			throw new WrongUserDataException("Wrong username or password.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
	}
	
	/**
	 * Getting a new password if the current was forgotten.
	 * @param user the user.
	 * @param newPassword the nem password.
	 * @return true if the change succesfull, false otherwise.
	 * @throws NoConnectionException if the connaction lost.
	 * @throws DecryptErrorException 
	 * @throws WrongUserDataException 
	 * @throws DataBaseErrorException 
	 * @throws EmailSendingException 
	 */
	public void forgotPassword(UserPOJO user, String newPassword) throws NoConnectionException, DecryptErrorException, WrongUserDataException, DataBaseErrorException, EmailSendingException
	{
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			ThriftUser thriftUser = new ThriftUser(user.getUserName(), user.getUserPasword());
			
			thriftExecutor.open();
			thriftExecutor.getExecutor().forgotPassword(thriftUser, newPassword);
		}
		catch (TDecryptErrorException e)
		{
			throw new DecryptErrorException("Error occured, on password decrypting.");
		}
		catch (TWrongUserDataException e)
		{
			throw new WrongUserDataException("Wrong username or password.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TEmailSendingException e)
		{
			throw new EmailSendingException("Email sending failed.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
	}
	
	/**
	 * Deleting a user.
	 * @param user the user.
	 * @return true if the deleting succesfull, false otherwise.
	 * @throws NoConnectionException if the connaction lost.
	 * @throws WrongUserDataException 
	 */
	public void deleteUser(UserPOJO user) throws NoConnectionException, WrongUserDataException, DataBaseErrorException
	{
		ThriftExecutor thriftExecutor = ThriftCLTFactory.getExecutor();
		
		try
		{
			ThriftUser thriftUser = new ThriftUser(user.getUserName(), user.getUserPasword());
			
			thriftExecutor.open();
			thriftExecutor.getExecutor().deleteUser(thriftUser);
		}
		catch (TWrongUserDataException e)
		{
			throw new WrongUserDataException("Wrong username or password.");
		}
		catch (TDatabaseErrorException e)
		{
			throw new DataBaseErrorException("Error occured in database.");
		}
		catch (TException e)
		{
			throw new NoConnectionException("Connection lost.");
		}
		finally
		{
			thriftExecutor.close();
		}
	}
}
